/**
* Copyright (C) 2018-2021
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.mall.biz.customer.controller;

import co.yixiang.common.core.constant.ShopConstants;
import co.yixiang.common.core.exception.BadRequestException;
import co.yixiang.common.log.annotation.Log;
import co.yixiang.common.mproot.domain.PageResult;
import co.yixiang.common.mproot.dozer.service.IGenerator;
import co.yixiang.mall.biz.customer.service.YxStoreCustomerService;
import co.yixiang.mall.common.aop.ForbidSubmit;
import co.yixiang.mall.common.customer.domain.YxStoreCustomer;
import co.yixiang.mall.common.customer.dto.YxStoreCustomerDto;
import co.yixiang.mall.common.customer.dto.YxStoreCustomerQueryCriteria;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
* @author Bug
* @date 2020-12-10
*/
@AllArgsConstructor
@Api(tags = "customer管理")
@RestController
@RequestMapping("/api/yxStoreCustomer")
public class YxStoreCustomerController {

    private final YxStoreCustomerService yxStoreCustomerService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("hasAnyAuthority('admin','yxStoreCustomer:list')")
    public void download(HttpServletResponse response, YxStoreCustomerQueryCriteria criteria) throws IOException {
        yxStoreCustomerService.download(generator.convert(yxStoreCustomerService.queryAll(criteria), YxStoreCustomerDto.class), response);
    }

    @GetMapping
    @Log("查询customer")
    @ApiOperation("查询customer")
    @PreAuthorize("hasAnyAuthority('admin','yxStoreCustomer:list')")
    public ResponseEntity<PageResult<YxStoreCustomerDto>> getYxStoreCustomers(YxStoreCustomerQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxStoreCustomerService.queryAll(criteria,pageable),HttpStatus.OK);
    }


    @Log("远程查询")
    @ApiOperation("查询customer")
    @GetMapping("/getCustomers")
    public ResponseEntity<List<YxStoreCustomer>> getCustomers(){
        List<YxStoreCustomer> yxStoreCustomers = yxStoreCustomerService.list(new LambdaQueryWrapper<YxStoreCustomer>().eq(YxStoreCustomer::getIsEnable, ShopConstants.YSHOP_ONE_NUM));
       return new ResponseEntity<>(yxStoreCustomers,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增customer")
    @ForbidSubmit
    @ApiOperation("新增customer")
    @PreAuthorize("hasAnyAuthority('admin','yxStoreCustomer:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxStoreCustomer resources){
        int count = yxStoreCustomerService.count(new LambdaQueryWrapper<YxStoreCustomer>().eq(YxStoreCustomer::getOpenId, resources.getOpenId()));
        if (count > 0) {
            throw new BadRequestException("当前用户已存在，请勿重复提交");
        }
        return new ResponseEntity<>(yxStoreCustomerService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改customer")
    @ForbidSubmit
    @ApiOperation("修改customer")
    @PreAuthorize("hasAnyAuthority('admin','yxStoreCustomer:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxStoreCustomer resources){
        yxStoreCustomerService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除customer")
    @ForbidSubmit
    @ApiOperation("删除customer")
    @PreAuthorize("hasAnyAuthority('admin','yxStoreCustomer:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        Arrays.asList(ids).forEach(id->{
            yxStoreCustomerService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
